/*
*	@file
*	@author  Thomas Kroes <t.kroes at tudelft.nl>
*	@version 1.0
*	
*	@section LICENSE
*	
*	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*	
*	Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*	Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*	Neither the name of the TU Delft nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
*	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*	
*	Part of the SSAO code is based on the tutorial by Michael Belanec: http://www.belanecbn.sk/opengl_tutorialy/index.php?id=30
*	The bloom filter code is based on code snippets from chapter 8 in "iPhone 3D Programming: http://ofps.oreilly.com/titles/9780596804824/chadvanced.html"
*
*/

#version 120

// Input textures
uniform sampler2D 	RenderTexture;
uniform sampler2D 	DepthTexture;
uniform sampler2D 	NormalTexture;
uniform sampler2D 	RandomTexture;
uniform mat4x4		Projection;
uniform mat4x4		ProjectionInverse;
uniform float		KernelRadius;
uniform int			KernelSize;
uniform float		InvKernelSize;
uniform vec3		KernelSamples[64];
uniform vec2		ScreenSize;
uniform vec2		InvScreenSize;
uniform vec2		RandomSize;
uniform vec2		InvRandomSize;

void main (void)
{
	gl_FragColor = vec4(vec3(1.0f, 1, 1), 0.5f);
	//return;
	
	float Depth = texture2D(DepthTexture, gl_TexCoord[0].st).r;

	float AccColor;
	
	if (Depth > 0.0f && Depth < 1.0)
	{
		vec4 Position = ProjectionInverse * vec4(gl_TexCoord[0].st, Depth, 1.0);
        Position.xyz /= Position.w;

        vec3 Normal = normalize(texture2D(NormalTexture, gl_TexCoord[0].st).rgb * 2.0 - 1.0);

        vec3 RandomUnitVector = normalize(texture2D(RandomTexture, gl_TexCoord[0].st * vec2(5.0)).rgb * 2.0 - 1.0);

        vec3 Tangent = normalize(RandomUnitVector - Normal * dot(RandomUnitVector, Normal));
        vec3 Binormal = cross(Normal, Tangent);

        mat3x3 RotationMatrix = mat3x3(Tangent, Binormal, Normal);
		
		for (int s = 0; s < KernelSize; s++)
		{
			vec3 SamplePosition = RotationMatrix * (KernelRadius * KernelSamples[s]) + Position.xyz;

			vec4 SamplePositionProjected = Projection * vec4(SamplePosition, 1.0);
			SamplePositionProjected.xyz /= SamplePositionProjected.w;

			float SampleDepth = texture2D(DepthTexture, SamplePositionProjected.st).r;

			if (SampleDepth < SamplePositionProjected.z)
			{
				vec4 SamplePositionUnprojected = ProjectionInverse * vec4(SamplePositionProjected.st, SampleDepth, 1.0);
				SamplePositionUnprojected.xyz /= SamplePositionUnprojected.w;
				
				vec3 p2p = SamplePositionUnprojected.xyz - Position.xyz;
				
				float Distance = dot(p2p, p2p);
				
				//if (Distance < KernelRadius)
				AccColor += 1.0 / (1.0f + Distance);
			}
		}

		AccColor = 1.0 - (AccColor * InvKernelSize);
		
		gl_FragColor = vec4(vec3(AccColor), 1.0f);
	}
}
